<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 7, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RLibImporter::cls( "xml.JSimpleXML" );
RCoreImporter::cls( "sysObjects.SysRequest" );

class ModulePositionManager extends Object {
	
	var $xml;
	
	/**
	 * 
	 *
	 * @param string $position - Position of Module
	 */
	function __construct($position) {
		unset ( $this->xml );
		$this->xml = new JSimpleXML ( );
		$file = $this->_getConfigFile ( $position );
		if ($file)
			$this->_loadFile ( $file );
		return;
	}
	
	/**
	 * Get config file follow position
	 *
	 * @param string $position
	 * @return unknown
	 */
	function _getConfigFile($position) {
		$fileXMLConfig = SYS_MOD_CONFIG_PATH . $position . '.pos.xml';
		
		if (file_exists ( $fileXMLConfig )) {
			return $fileXMLConfig;
		}
		$this->setError ( "Module config file: <b>$fileXMLConfig</b> doesn't exists." );
		RError::raiseWarning ( 300, $this->getError () );
		return false;
	}
	
	/**
	 * load file XML
	 *
	 * @param string $filePath - Path to XML file
	 * @return unknown
	 */
	function _loadFile($filePath) {
		return $this->xml->loadFile ( $filePath );
	}
	
	/**
	 * Get Children Node of XML document
	 *
	 * @return XML Tag
	 */
	function _getChildren() {
		if (! empty ( $this->xml->document ))
			return $this->xml->document->children ();
		return false;
	}
	
	/**
	 * Get Tag of View
	 *
	 * @param XML Tag $XMLTag
	 */
	function _getTagModules($XMLTag) {
		$numberOfModule = count ( $XMLTag );
		for($i = 0; $i < $numberOfModule; $i ++) {
			$elementOfModule = $XMLTag [$i]->children ();
			$modName = $XMLTag [$i]->attributes ();
			
			$moduleName = $modName ['name'];
			
			$numberOfElement = count ( $elementOfModule );
			if ($numberOfElement == 0) {
				$this->_printModule ( $moduleName, $moduleParams );
				continue;
			}
			
			$viewElementOfModule = $this->_getFirstNode ( $elementOfModule, "views" );
			if (! empty ( $viewElementOfModule )) {
				$viewElementOfModule = $viewElementOfModule->children ();
				if (! ($this->canViewModule ( $viewElementOfModule ))) {
					continue;
				}
			}
			
			$paramsOfModule = $this->_getFirstNode ( $elementOfModule, "params" );
			if (! empty ( $paramsOfModule )) {
				$paramsOfModule = $paramsOfModule->children ();
				$moduleParams = $this->_getModuleParams ( $paramsOfModule );
			}
			
			if (empty ( $moduleName )) {
				// Content module
				$contentOfModule = $this->_getFirstNode ( $elementOfModule, "content" );
				$contentOfModule = $contentOfModule->data ();
				
				if (empty ( $paramsOfModule )) {
					echo $contentOfModule;
					continue;
				}
				
				foreach ( $paramsOfModule as $param ) {
					$paraName = $param->name ();
					$text = "{{$paraName}}";
					if ($param->attributes ( 'type' ) == "sys_constant") {
						eval ( '$val = ' . $param->name () . ';' );
						$contentOfModule = str_replace ( $text, $val, $contentOfModule );
					}
				}
				
				echo $contentOfModule;
				continue;
			}
			
			$this->_printModule ( $moduleName, $moduleParams );
		}
	}
	
	/**
	 * Get the first node with the $nodeName in the $listNode
	 *
	 * @param array XML element $listNode
	 * @param string $nodeName
	 * @return JSimpleXMLElement
	 */
	function _getFirstNode($listNode, $nodeName) {
		foreach ( $listNode as $node ) {
			if ($node->name () == $nodeName) {
				return $node;
			}
		}
		return null;
	}
	
	/**
	 * Get Data from XMLTag
	 *
	 * @param JSimpleXMLElement Object $XMLTag
	 * @return string data
	 */
	function getData($XMLTag) {
		return $XMLTag->_data;
	}
	
	/**
	 * Print data from XML Tag
	 *
	 * @param JSimpleXMLElement Object $XMLTag
	 */
	function printData($XMLTag) {
		echo $this->getData ( $XMLTag );
	}
	
	/**
	 * Show module
	 *
	 * @param string $moduleName example: 'login'
	 * @param array $moduleParams - Parameters to use in class AbstractModule
	 */
	function _printModule($moduleName, $moduleParams = null) {
		$module = &SystemSite::getModule($moduleName, $moduleParams);
		if ($module->isView ()) {
			$module->printModule ();
		}
	}
	
	/**
	 * Show all Modules
	 *
	 * @return unknown
	 */
	function showModules() {
		if ($xmlTag = $this->_getChildren ())
			return $this->_getTagModules ( $xmlTag );
		return false;
	
	}
	
	/**
	 * Get Parameters of Module
	 *
	 * @param array of JSimpleXMLElement Object $paramElement
	 * @return Object contain Parameters
	 */
	function _getModuleParams($paramElement) {
		$numberOfParamElement = count ( $paramElement );
		$name = new stdClass ( );
		for($j = 0; $j < $numberOfParamElement; $j ++) {
			$paramName = $paramElement [$j]->_name;
			$name->$paramName = $paramElement [$j]->_data;
		}
		return $name;
	}
	
	/**
	 * Check the module can view or not
	 *
	 * @param array of JSimpleXMLElement $viewElement
	 * 
	 * @return boolean
	 */
	function canViewModule($viewElement) {
		$numberOfViewElement = count ( $viewElement );
		
		$hasDisappear = false;
		for($j = 0; $j < $numberOfViewElement; $j ++) {
			$viewArr = $viewElement [$j]->attributes ();
			
			$sysResource = SysRequest::getResource ();
			$menuResource = new SiteResource ( );
			$menuResource->setValueFromRequest ( $viewArr );
			if (! empty ( $viewArr ['group'] )) {
				$userGroup = explode ( ',', $viewArr ['group'] );
			}
			
			if ($viewElement [$j]->name () == 'disappear') {
				if (empty ( $userGroup ) || ! $this->_checkUserGroup ( $userGroup )) {
					if ($sysResource->checkCover ( $menuResource )) {
						return false;
					}
				}
				$hasDisappear = true;
			} else if ($viewElement [$j]->name () == 'appear') {
				if (empty ( $userGroup ) || $this->_checkUserGroup ( $userGroup )) {
					if ($sysResource->checkCover ( $menuResource )) {
						return true;
					}
				}
			}
		}
		return $hasDisappear;
	}
	
	/**
	 * Check Current user in Group?
	 *
	 * @param array of UserGroup $arrGroups
	 * @return true|false - true if user in group
	 */
	function _checkUserGroup($arrGroups) {
		//the group check.
		if (class_exists('SysAuthenticate')) {
			$userType = SysAuthenticate::getUserType();
			if (in_array($userType, $arrGroups)) {
				return true;
			}
			
			return false;
		}
		return true;
	}
}

?>